var pageSize = 10;
//职称类别
var positionCodeMap = {};

$(document).ready(function(){
	commonSet();
	//表格初始化
	initTable();
	//事件绑定
	eventBind();
});

function commonSet(){
	initLoader();
	genDictSelect('positionCode','position_code','1');
	positionCodeMap =getTransParam('positionCode');
}

//事件绑定
function eventBind(){
	//带有分页信息的查询
	$("#queryBtn").click(pageQuery).click();
	
	//添加按钮弹出层
	$("#addBtn").click(showAddFormDialog);
}

//表格初始化
function initTable(){
	$('#costListTable').bootstrapTable({
		onPageChange: function (number, size) {
			pageSize = size;
			query(number, size);
		}
	});
}

//带有分页信息的查询
function pageQuery(){
	query(1, pageSize);
};

//分页查询
function query(pageNumber, pageSize){
	var queryCondition={
		pageNumber: pageNumber, 
		pageSize: pageSize,
		params:$("#costForm").collect()
	}
	
	$.getApiClient('/cost/list').setQuery(queryCondition).post().done(function(data){
	    $('#costListTable').bootstrapTable('load', data.payload);
	});
};

//添加医院弹出
function showAddFormDialog(){
	customRemoteUrlDialog('添加费用','/page/business/cost/form?type=add',null);
}

//修改医院弹出
function showEditFormDialog(row){
	customRemoteUrlDialog('修改费用','/page/business/cost/form?type=edit&costId='+row.id,null);
}

//删除医院弹出
function showDelFormDialog(row){
	customConfirmDialog('是否确认删除费用？','删除费用',null,function(dialog){
		//执行删除
		dialog.close();
		doDeleteHos(row.id,function(data){
			if(data.code == "0"){
				customTimeOutDialog('信息提示','删除费用成功',null,2000);
				pageQuery();
			}else{
				customAlertDialog(data.message, '错误提示', null, null);
			}
		})
	});
	
	function doDeleteHos(costId,callBack){
		$.getApiClient("/cost/delete/{id}").setQuery({
			id : costId
		}).del().done(function(data){
			callBack(data)
		});
	}
}

//操作列
function operateFormatter(value, row, index) {	
	var operates=[];
	operates.push('<a class="edit" href="javascript:void(0)" title="修改"><i class="fa fa-edit"></i>修改</a>');
	//医院管理员不能删除自己医院信息
	if(loginUser.user_type!='2'){
		operates.push('&nbsp;&nbsp;&nbsp;');
		operates.push('<a class="delete" href="javascript:void(0)" title="删除"><i class="fa fa-close"></i>删除</a>');
	}
	return operates.join('');
};

operateEvents = {
	'click .edit' : function(e, value, row, index) {
		showEditFormDialog(row);
	},
	'click .delete' : function(e, value, row, index) {
		showDelFormDialog(row);
	}
};